package com.springboot.controller;

import com.springboot.dto.ChatContentView;
import com.springboot.dto.FriendsList;
import com.springboot.model.AssistantListFormKey;
import com.springboot.model.ChatRecord;
import com.springboot.model.UserLoginForm;
import com.springboot.service.AssistantService;
import com.springboot.service.FileUploadService;
import com.springboot.service.OnlineConsultationService;
import com.springboot.service.UserLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@Controller
public class OnlineConsultation {
    @Autowired
    UserLoginService userLoginService;
    @Autowired
    AssistantService assistantService;
    @Autowired
    OnlineConsultationService onlineConsultationService;
    @Autowired
    FileUploadService fileUploadService;

    /**
     * 助理查看聊天的好友列表
     */
    @CrossOrigin
    @RequestMapping(value = "/assistantShowMessage")
    public @ResponseBody
    String showFriendsList(@RequestParam("assistantTel")  String tel){
        System.out.println("开始显示聊天好友列表");

        try{
            UserLoginForm userLoginForm=userLoginService.findUserByTel(tel);
            Integer assistantId=userLoginForm.getUserId();
            ArrayList<FriendsList> friendsLists=new ArrayList<>();
            ArrayList<AssistantListFormKey> assistantListFormKeys = new ArrayList<>();
            assistantListFormKeys=assistantService.findDoctorsByID(assistantId);
            System.out.println(assistantId+" "+assistantListFormKeys.size());
            for (int i=0;i<assistantListFormKeys.size();i++){
                int doctorId=assistantListFormKeys.get(i).getDoctorId();
                System.out.println("医生ID："+doctorId);
                ArrayList<FriendsList> friendsLists1=onlineConsultationService.showFriendList(doctorId,assistantId);
                for (int j=0;j<friendsLists1.size();j++){
                    friendsLists.add(friendsLists1.get(j));
                }
            }
            if (friendsLists.size()==0)
            {
                return "空";
            }
            System.out.println("结束查询");
            return friendsLists.toString();
        }
        catch (Exception e){
            return "Flase";
        }

    }

    @CrossOrigin
    @RequestMapping(value = "/showCustomerServiceNum")
    public @ResponseBody
    String showCustomerServiceNum(){
        System.out.println("开始客服未读数量");
        FriendsList friendsList=new FriendsList();
        friendsList=onlineConsultationService.getCustomerServiceInfo();
        System.out.println(friendsList.toString1());
        return friendsList.toString1();
    }

    /**
     * 助理查看自己充当客服时的聊天好友列表（由上面的好友列表点击进入）
     */
    @CrossOrigin
    @RequestMapping(value = "/showCustomerServiceFriendsList")
    public @ResponseBody
    String showCustomerServiceFriendsList(){
        System.out.println("开始显示客服的聊天好友列表");
        try{
            ArrayList<FriendsList> friendsLists=new ArrayList<>();
            friendsLists=onlineConsultationService.showCustomerServiceList();
            System.out.println("展示结束");
            if(friendsLists.size()==0){
                return "空";
            }
            System.out.println("结束显示");
            return friendsLists.toString();
        }
        catch (Exception e){
            return "Flase";
        }

    }

    /**
     * 助理查看聊天内容，并把消息标记为已读
     */
    @CrossOrigin
    @RequestMapping(value = "/showChatContent")
    public @ResponseBody
    String showChatContent(@RequestParam("tel")  String tel,//助理电话
                           @RequestParam("doctorTel")  String doctorTel,
                           @RequestParam("otherTel")  String otherTel){//患者电话
        System.out.println("开始展示聊天内容");
        //当doctorTel为空，助理为客服；当为otherTel空，助理与医生聊天
        ArrayList<ChatContentView> chatContentViews=new ArrayList<>();
        System.out.println(tel+" |"+doctorTel+"| "+otherTel);
        try{
            if(doctorTel.equals("null")){
                System.out.println("助理充当客服，与患者聊天");
                UserLoginForm userLoginForm=userLoginService.findUserByTel(tel);
                Integer assistantId=userLoginForm.getUserId();
                userLoginForm=userLoginService.findUserByTel(otherTel);
                Integer patientId=userLoginForm.getUserId();
                try{
                    chatContentViews=onlineConsultationService.showChatContent(assistantId,patientId);
                }
                catch (Exception e){
                    e.printStackTrace();
                }
            }
            else if(otherTel.equals("null")){
                System.out.println("助理与医生聊天");
                UserLoginForm userLoginForm=userLoginService.findUserByTel(tel);
                Integer assistantId=userLoginForm.getUserId();
                userLoginForm=userLoginService.findUserByTel(doctorTel);
                Integer doctorId=userLoginForm.getUserId();
                chatContentViews=onlineConsultationService.showChatContent1(assistantId,doctorId);
            }
            else if(!doctorTel.equals("null")&&(!otherTel.equals("null"))){
                System.out.println("助理本人与患者聊天");
                System.out.println(tel+" "+doctorTel+" "+otherTel);
                UserLoginForm userLoginForm=userLoginService.findUserByTel(tel);
                Integer assistantId=userLoginForm.getUserId();
                userLoginForm=userLoginService.findUserByTel(doctorTel);
                Integer doctorId=userLoginForm.getUserId();
                userLoginForm=userLoginService.findUserByTel(otherTel);
                Integer patientId=userLoginForm.getUserId();
                try {
                    chatContentViews=onlineConsultationService.showChatContent2(assistantId,doctorId,patientId);
                }
                catch (Exception e){
                    e.printStackTrace();
                }
            }
            else {
                System.out.println("传参错误");
                return "false";
            }
            if (chatContentViews.size()==0){
                return "空";
            }
            return chatContentViews.toString();
        }
        catch (Exception e){
            return "false";
        }
    }



    /**
     * 助理发送文本信息
     */
    @CrossOrigin
    @RequestMapping(value = "/assistantSendTextMessage")
    public @ResponseBody
    String assistantSendMessage(@RequestParam("tel")  String tel,
                                @RequestParam("doctorTel")  String doctorTel,
                                @RequestParam("otherTel")  String otherTel,
                                @RequestParam("content")  String content){
        System.out.println("开始展示聊天内容");
        //当doctorTel为空，助理为客服；当为otherTel空，助理与医生聊天
        try{
            ChatRecord chatRecord=new ChatRecord();
            Date now = new Date();
            java.util.Date d = null;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String str = sdf.format(now);//今日日期
            System.out.println(str);
            try {
                d = sdf.parse(str);
            } catch (Exception e) {
                e.printStackTrace();
                return "false";
            }
            java.sql.Date nowdate = new java.sql.Date(d.getTime());
            System.out.println(str+"|| "+nowdate);
            String logoLink=null;
            int isImg1=0;
            chatRecord.setChatContent(content);
            chatRecord.setChatTime(nowdate);
            chatRecord.setFlag2(isImg1);
            chatRecord.setReceiverState("未读");
            chatRecord.setPhotoLink(logoLink);
            chatRecord.setPatientFlag(0);
            chatRecord.setDoctorFlag(0);
            chatRecord.setAssistantFlag(0);
            if(doctorTel.equals("null")){
                UserLoginForm userLoginForm=userLoginService.findUserByTel(tel);
                Integer assistantId=userLoginForm.getUserId();
                userLoginForm=userLoginService.findUserByTel(otherTel);
                Integer patientId=userLoginForm.getUserId();
                chatRecord.setSenderId(0);
                chatRecord.setReceiverId(patientId);
                chatRecord.setFlag1(1);
                chatRecord.setAssistantId(assistantId);
            }
            else if(otherTel.equals("null")){
                UserLoginForm userLoginForm=userLoginService.findUserByTel(tel);
                Integer assistantId=userLoginForm.getUserId();
                userLoginForm=userLoginService.findUserByTel(doctorTel);
                Integer doctorId=userLoginForm.getUserId();
                chatRecord.setFlag1(1);
                System.out.println(assistantId+" "+tel);
                chatRecord.setSenderId(assistantId);
                chatRecord.setReceiverId(doctorId);
            }
            else {
                UserLoginForm userLoginForm=userLoginService.findUserByTel(tel);
                Integer assistantId=userLoginForm.getUserId();
                userLoginForm=userLoginService.findUserByTel(doctorTel);
                Integer doctorId=userLoginForm.getUserId();
                userLoginForm=userLoginService.findUserByTel(otherTel);
                Integer patientId=userLoginForm.getUserId();
                chatRecord.setSenderId(doctorId);
                chatRecord.setReceiverId(patientId);
                chatRecord.setFlag1(0);
                chatRecord.setAssistantId(assistantId);
            }
            int a=onlineConsultationService.insertChatContent(chatRecord);
            System.out.println(a+"  "+chatRecord.toString());
            return "true";
        }
        catch (Exception e){
            return "false";
        }
    }

    /**
     * 助理发送图片信息
     */
    @CrossOrigin
    @RequestMapping(value = "/assistantSendPictureMessage")
    public @ResponseBody
    String assistantSendMessage(@RequestParam("tel")  String tel,
                                @RequestParam("doctorTel")  String doctorTel,
                                @RequestParam("otherTel")  String otherTel,
                                @RequestParam("content1") MultipartFile file){
        System.out.println("开始展示聊天内容");
        //当doctorTel为空，助理为客服；当为otherTel空，助理与医生聊天
        try{
            ChatRecord chatRecord=new ChatRecord();
            Date now = new Date();
            java.util.Date d = null;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String str = sdf.format(now);//今日日期
            System.out.println(str);
            try {
                d = sdf.parse(str);
            } catch (Exception e) {
                e.printStackTrace();
                return "false";
            }
            java.sql.Date nowdate = new java.sql.Date(d.getTime());
            System.out.println(str+"|| "+nowdate);
            String logoLink=null;
            int isImg1=0;
            logoLink =fileUploadService.UploadPicture(file,"personInfotel="+tel);//照片存储路径
            isImg1=1;
            chatRecord.setChatTime(nowdate);
            chatRecord.setFlag2(isImg1);
            chatRecord.setReceiverState("未读");
            chatRecord.setPhotoLink(logoLink);
            chatRecord.setPatientFlag(0);
            chatRecord.setDoctorFlag(0);
            chatRecord.setAssistantFlag(0);
            if(doctorTel.equals("null")){
                UserLoginForm userLoginForm=userLoginService.findUserByTel(tel);
                Integer assistantId=userLoginForm.getUserId();
                userLoginForm=userLoginService.findUserByTel(otherTel);
                Integer patientId=userLoginForm.getUserId();
                chatRecord.setSenderId(0);
                chatRecord.setReceiverId(patientId);
                chatRecord.setFlag1(1);
                chatRecord.setAssistantId(assistantId);
            }
            else if(otherTel.equals("null")){
                UserLoginForm userLoginForm=userLoginService.findUserByTel(tel);
                Integer assistantId=userLoginForm.getUserId();
                userLoginForm=userLoginService.findUserByTel(doctorTel);
                Integer doctorId=userLoginForm.getUserId();
                chatRecord.setFlag1(1);
                chatRecord.setSenderId(assistantId);
                chatRecord.setReceiverId(doctorId);
            }
            else {
                UserLoginForm userLoginForm=userLoginService.findUserByTel(tel);
                Integer assistantId=userLoginForm.getUserId();
                userLoginForm=userLoginService.findUserByTel(doctorTel);
                Integer doctorId=userLoginForm.getUserId();
                userLoginForm=userLoginService.findUserByTel(otherTel);
                Integer patientId=userLoginForm.getUserId();
                chatRecord.setSenderId(doctorId);
                chatRecord.setReceiverId(patientId);
                chatRecord.setFlag1(0);
                chatRecord.setAssistantId(assistantId);
            }
            int a=onlineConsultationService.insertChatContent(chatRecord);
            System.out.println(a+"  "+chatRecord.toString());
            return "true";
        }
        catch (Exception e){
            return "false";
        }
    }

    @CrossOrigin
    @RequestMapping(value = "/assistantDeleteChat")
    public @ResponseBody
    String assistantSendMessage(@RequestParam("tel")  String tel,
                                @RequestParam("doctorTel")  String doctorTel,
                                @RequestParam("otherTel")  String otherTel){
        System.out.println("开始删除联系人");
        try{
            int result=0;
            if(doctorTel.equals("")) {
                UserLoginForm userLoginForm = userLoginService.findUserByTel(tel);
                Integer assistantId = userLoginForm.getUserId();
                userLoginForm = userLoginService.findUserByTel(otherTel);
                Integer patientId = userLoginForm.getUserId();
                result=onlineConsultationService.deleteContactPerson(0,patientId);
            }
            else if(otherTel.equals("")) {
                UserLoginForm userLoginForm = userLoginService.findUserByTel(tel);
                Integer assistantId = userLoginForm.getUserId();
                userLoginForm = userLoginService.findUserByTel(doctorTel);
                Integer doctorId = userLoginForm.getUserId();
                result=onlineConsultationService.deleteContactPerson(assistantId,doctorId);
            }
            else{
                UserLoginForm userLoginForm=userLoginService.findUserByTel(tel);
                Integer assistantId=userLoginForm.getUserId();
                userLoginForm=userLoginService.findUserByTel(doctorTel);
                Integer doctorId=userLoginForm.getUserId();
                userLoginForm=userLoginService.findUserByTel(otherTel);
                Integer patientId=userLoginForm.getUserId();
                result=onlineConsultationService.deleteContactPerson(doctorId,patientId);
            }
            System.out.println(result+" 删除成功");
            return "true";
        }
        catch (Exception e){
            return "false";
        }
    }

}
